/***
 * author: Adam Pociejowski
 */

package requests.music;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

import requests.Controller;
import requests.FileFinder;
import requests.IRequest;
import requests.settings.Settings;
import main.DemiGui;

public class Mp3Browser implements IRequest, ActionListener {
	
	private Controller controller;
	private DemiGui instance;
	private FileFinder finder;
	private String path;
	private int heigth, width;
	private JFrame frame;
	private JPanel table;
	private JButton[] buttons;
	private JScrollPane pane;
	private Color buttonsColor, buttonsColor2;
	
	public Mp3Browser(Controller controller, DemiGui instance, FileFinder finder, String path, int width, int heigth, Settings settings) {
		
		this.instance = instance;
		this.controller = controller;
		this.finder = finder;
		this.path = path;
		this.heigth = heigth;
		this.width = width;
		String content = settings.getSettings("mp3BrowserButtonsColor");
		buttonsColor = getColor(content);
		content = settings.getSettings("mp3BrowserButtonsColor2");
		buttonsColor2 = getColor(content);
		
	}
	
	public void showList() {
		
		/********         Simple GUI for mp3 browser            **********/
		
		frame = new JFrame();
		frame.setSize(width, heigth);
		frame.setTitle("D.E.M.I - Mp3 Browser");
		frame.setVisible(true);
		frame.setResizable(false);
		
		table = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		
		String list[] = finder.Find(path, "");
		buttons = new JButton[list.length];
		
		for (int i = 0; i < list.length; i++) {
			
			buttons[i] = new JButton(list[i]);
			buttons[i].setHorizontalAlignment(SwingConstants.LEFT);
			buttons[i].setOpaque(true);
			buttons[i].addActionListener(this);
			c.gridy = i;
			if (i%2 == 0) 
				buttons[i].setBackground(buttonsColor);
			else 
				buttons[i].setBackground(buttonsColor2);
			table.add(buttons[i], c);
		}
		
		frame.add(table);
		pane = new JScrollPane(table);
		frame.add(pane);
		
		/*****************************************************************/
		
	}
	
	Color getColor(String content) {
		int tmp[] = new int[3];
		tmp[0] = Integer.parseInt(content.substring(0,3));
		tmp[1] = Integer.parseInt(content.substring(4,7));
		tmp[2] = Integer.parseInt(content.substring(8,11));
		Color color = new Color(tmp[0],tmp[1],tmp[2]);
		return color;
	}
	
	
	@Override
	public void returnDEMI(String text) {
		instance.Monitor(text);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		/*** Cutting song title from (String)e.getSource  String ****/
		
		String src = ""+e.getSource();
		int index = src.indexOf("text");
		if (index == -1) {
			returnDEMI("Mp3Browser - action Performed Error");
		}
		else {
			String tmp = src.substring(index+5, src.length() - 25);
			controller.task("m "+tmp);		
		}	
		
		/*************************************************************/
	}

}
